//
//  YDAuthManager.m
//  IngCard
//
//  Created by kf on 2017/9/6.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDAuthManager.h"
#import "YDHttpClient.h"
#import "YDAccountManager.h"
#import "YDAPI.h"

@implementation YDAuthManager

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static YDAuthManager *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}



-(void) authBindAccount:(YD3rdSignID) signID
            accessToken:(NSString *)accessToken
                 mobile:(NSString *) mobile
             verifyCode:(NSString *) verifyCode
             onComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.auth_bind parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:@(signID) forKey:@"thirdSignId"];
        [parameters safeSetObject:accessToken forKey:@"openId"];
        [parameters safeSetObject:mobile forKey:@"mobile"];
        [parameters safeSetObject:verifyCode forKey:@"password"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        
        if(success)
        {
            ACCOUNT.account.uid = NSStringMake(response[@"uid"]);
            ACCOUNT.account.mobile = NSStringMake(response[@"mobile"]);
            ACCOUNT.account.login_token = NSStringMake(response[@"login_token"]);
        }
        BlockGoGo(onComplete,success, msg);
    }];
}

-(void) authCheckBinding:(YD3rdSignID) signID
             accessToken:(NSString *) accessToken
              onComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.auth_callback parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:@(signID) forKey:@"thirdSignId"];
        [parameters safeSetObject:accessToken forKey:@"openId"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        
        if (success)
        {
            ACCOUNT.account.uid = NSStringMake(response[@"uid"]);
            ACCOUNT.account.mobile = NSStringMake(response[@"mobile"]);
            ACCOUNT.account.login_token = NSStringMake(response[@"login_token"]);
            
            [ACCOUNT getAccountDetail:^(BOOL success, NSString *msg) {
                if(success)
                {
                    BlockGoGo(onComplete, success, msg);
                    NSNotificationMake(ACCOUNT.Notification_userLogin, nil);
                }
            }];
        }
        else
        {
            BlockGoGo(onComplete, success, msg);
        }
    }];
}

-(void) get3rdAccountDetail:(YD3rdSignID) signID
                accessToken:(NSString *) accessToken
                        uid:(NSString *)uid
                 loginToken:(NSString *) loginToken
                 onComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.auth_detail parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:@(signID) forKey:@"thirdSignId"];
        [parameters safeSetObject:accessToken forKey:@"openId"];
        [parameters safeSetObject:uid forKey:@"uid"];
        [parameters safeSetObject:loginToken forKey:@"loginToken"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        if (success)
        {
            self.linkedInAccount = [YDLinkedInAccount serializeWith:response];
        }
        
        BlockGoGo(onComplete, success, msg);
    }];
}

-(void) authBindingAccountDetail:(YD3rdSignID) signID
                     accessToken:(NSString *) accessToken
                             uid:(NSString *)uid
                      loginToken:(NSString *) loginToken
                      onComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.auth_registerDetail parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:@(signID) forKey:@"thirdSignId"];
        [parameters safeSetObject:accessToken forKey:@"openId"];
        [parameters safeSetObject:uid forKey:@"uid"];
        [parameters safeSetObject:loginToken forKey:@"login_token"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        
        [ACCOUNT getAccountDetail:^(BOOL success, NSString *msg) {
            BlockGoGo(onComplete ,success, msg);
            NSNotificationMake(ACCOUNT.Notification_userLogin, nil);
        }];
    }];
    
}


@end
